﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using ZFSDomain.Interface.Base;
using ZFSDomain.SysModule;

namespace ZFSDomain.ViewDialog
{
    public class BaseViewDialog<T> : IModelDialog where T : new()
    {
        public T TView;

        public virtual void BindViewModel<TViewModel>(TViewModel viewModel)
            where TViewModel : BaseDialogOperation
        {
            var dialog = GetDialog();
            (dialog as UserControl).DataContext = viewModel;
        }

        public object GetDialog()
        {
            if (TView == null) TView = new T();
            return TView as object;
        }

        public async virtual Task<bool> ShowDialog(
            DialogOpenedEventHandler openedEventHandler, 
            DialogClosingEventHandler closingEventHandler)
        {
            var dialog = GetDialog();
            object taskResult = await DialogHost.Show(dialog, "RootDialog", 
                openedEventHandler, closingEventHandler); //位于顶级窗口
            return (bool)taskResult;
        }

        public virtual void Close() { }

        public bool GetResult()
        {
            return (
                (
                (GetDialog() as UserControl).DataContext)
                as BaseDialogOperation
                ).Result;
        }
    }
}
